<?php

namespace app\common\model;

use app\helpers\Common;
use app\libs\traits\BaseModelTrait;

use think\model\concern\SoftDelete;

class Crontab extends Base
{
    /**
     * Base Model Trait
     */
    use BaseModelTrait;

    //use SoftDelete;

    protected $deleteTime = 'deleted_at';

    // 软删除字段默认值
    protected $defaultSoftDelete = 0;

    // 当前模型名称 不带前缀
    protected $name = 'crontab';

    public $pk = 'id';

    public $types = [
        'url' => '请求URL',
        'sql' => '执行SQL',
        'shell' => '执行Shell',
    ];

    protected function getBegintimeAttr($value)
    {
        return $value && is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
    }

    protected function getEndtimeAttr($value)
    {
        return $value && is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
    }

    /**
     * 格式化列表数据
     */
    public static function formatDatas($list, $params = '')
    {
        foreach ($list as &$item) {
            $item = (new self())->formatItem($item, $params);
        }

        return $list;
    }

    /**
     * 格式化单个数据
     */
    public static function formatItem($item, $params = '')
    {
        if (!$item) {
            return '';
        }

        if (isset($params['format_source']) && $params['format_source'] == 'backend') {
            // 类型
            $item['type_name'] = (new self())->types[$item['type']];
            // 最大执行次数
            if ($item['maximums'] == 0) {
                $item['maximums_name'] = '不限';
            } else {
                $item['maximums_name'] = $item['maximums'];
            }
            // 下次预计时间
            $item['next_time'] = 0;
        }

        return $item;
    }
}
